package org.ycsoft.PathComputation;

/**
 * Represents a connection between 2 stations
 * @author allad
 *
 */
public class Edge {
    private Stop departure;
    private Stop arrival;
    
    private int length; //number of seconds 
    private Line line;
    
    /**
     * @param line The Line object which this segment is part of. If null, the edge is considered to be a foot edge
     * @param departure The departure stop
     * @param arrival The arrival stop
     * @param length The travel time in seconds
     */
    public Edge(Line line,Stop departure,Stop arrival,int length){
        this.line = line;
        this.departure = departure;
        this.arrival = arrival;
        this.length = length;
    }
    
    /**
     * @return the departure stop
     */
    public Stop getDeparture(){
        return departure;
    }
    
    /**
     * @return the arrival stop
     */
    public Stop getArrival(){
        return arrival;
    }
    
    /**
     * @return the Line object which this segment is part of
     */
    public Line getLine(){
        return line;
    }
    
    /**
     * @return the time in seconds required to travel this edge
     */
    public int getLength(){
        if(length > 0){
        	return length;
        }
        else if(length == 0){
        	return 5*60; //by default return 5 min
        }
        //if length is < 0
        else if(line != null){
        	return line.getAverageTimePerStop();
        }
        else return 5*60;
    }
    
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString(){
    	return "["+departure+" -> "+arrival+"]";
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object o){
    	if(o == null){
    		return false;
    	}
    	if(o instanceof Edge){
    		Edge e = (Edge)o;
    		if(e.departure.equals(this.departure) && e.arrival.equals(this.arrival) && e.getLength() == this.getLength()){
    			return true;
    		}
    		else return false;
    	}
    	else return false;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    public int hashCode(){
    	String tmp = departure.hashCode()+"-"+arrival.hashCode();
    	return tmp.hashCode();
    }
} 
 
